﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;

namespace Heaven.Diagnostics
{
    /// <summary>
    /// This class helps us to count 
    /// execution time accurately
    /// </summary>
    public class PerformanceCounter
    {
        #region Fields

        Dictionary<string, double> counters = new Dictionary<string, double>();
        Stopwatch stopwatch = new Stopwatch();
        string currentName;

        #endregion

        #region Properties

        /// <summary>
        /// Indexer
        /// </summary>
        /// <param name="name">Name of counter</param>
        /// <returns>Time of execution, ms</returns>
        public double this[string name]
        {
            get { return counters[name]; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Starts a new counter
        /// </summary>
        /// <param name="name">
        /// Name of the counter</param>
        public void Start(string name)
        {
            stopwatch.Reset();
            stopwatch.Start();
            currentName = name;
        }

        /// <summary>
        /// Ends counter process
        /// </summary>
        public void End()
        {
            stopwatch.Stop();
            counters.Add(currentName, ((double)stopwatch.ElapsedTicks / (double)Stopwatch.Frequency) * 1000.0);
        }

        #endregion
    }
}
